﻿namespace MobileTrack.Web
{
    using System;
    using System.Linq;
    using System.Web.Security;
    using MobileTrack.Core;

    public static class Extensions
    {
        public static string GetRoleForUser(this RoleProvider roleProvider, string userName)
        {
            string role = string.Empty;
            if (roleProvider != null)
            {
                role = roleProvider
                    .GetRolesForUser(userName)
                    .FirstOrDefault(r => !r.EndsWith(Constants.SucursalRoleSuffix, StringComparison.OrdinalIgnoreCase));
            }

            return role;
        }

        public static string GetSucursalRoleForUser(this RoleProvider roleProvider, string userName)
        {
            string sucursalRole = string.Empty;
            if (roleProvider != null)
            {
                sucursalRole = roleProvider
                    .GetRolesForUser(userName)
                    .FirstOrDefault(r => r.EndsWith(Constants.SucursalRoleSuffix, StringComparison.OrdinalIgnoreCase));
                if (!string.IsNullOrWhiteSpace(sucursalRole))
                {
                    sucursalRole = sucursalRole.Replace(Constants.SucursalRoleSuffix, string.Empty);
                }
            }
            
            return sucursalRole;
        }
    }
}